import 'package:camera/camera.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:squat_easy/generated/assets.dart';
import 'package:squat_easy/se_main/se_video/record/se_video_record_logic.dart';
import 'package:squat_easy/se_route.dart';
import 'package:squat_easy/se_ui/se_dialogs.dart';
import 'package:squat_easy/se_utils/se_time_utils.dart';
import 'package:ylm_ui_kit/ylm_ui_kit.dart';

class SeVideoRecordPage extends StatefulWidget {
  const SeVideoRecordPage({super.key});

  @override
  State<SeVideoRecordPage> createState() => _SeVideoRecordPageState();
}

class _SeVideoRecordPageState extends State<SeVideoRecordPage> {
  late final SeVideoRecordLogic logic;

  @override
  void initState() {
    super.initState();
    logic = Get.put(SeVideoRecordLogic());
  }

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: () async => false,
      child: BaseScaffold(
        hideBackBtn: true,
        child: GetBuilder<SeVideoRecordLogic>(
          assignId: true,
          builder: (controller) {
            return Stack(
              children: [
                Column(
                  children: [
                    _buildContent().expanded(),
                    _buildBottom(),
                  ],
                ),
                _buildAppbar(),
              ],
            );
          },
        ),
      ),
    );
  }

  Widget _buildAppbar() {
    return Container(
      padding: EdgeInsets.only(top: ScreenUtil().statusBarHeight),
      child: Container(
        height: 44,
        alignment: Alignment.center,
        padding: EdgeInsets.symmetric(horizontal: 20.w),
        child: Row(
          children: [
            _buildBack(),
            _buildDuration().paddingHorizontal(25.w).expanded(),
            _buildCount(),
          ],
        ),
      ),
    );
  }

  Widget _buildBack() {
    return ButtonWidget.icon(
      Assets.svgAppBarBack.toSvg(width: 42),
      onTap: () {
        SeDialogs.twoButton(
          title: "Are you sure you want to quit? Your video won't be saved.",
          cancelTitle: "No",
          confirmTitle: "Yes",
          onConfirm: SeRoute.close,
        );
      },
    );
  }

  Widget _buildDuration() {
    return Container(
      decoration: CommonDecoration.getShapeDecoration(
        bg: '#626161'.toColor,
        borderWidth: 1,
        borderColor: Colors.white,
        radius: 12,
      ),
      alignment: Alignment.center,
      child: Row(
        mainAxisSize: MainAxisSize.min,
        children: [
          Obx(() {
            return TextWidget.oneLine(
              text: SeTimeUtils.formatSecondsToHMS(logic.duration.value),
              color: Colors.white,
              fontSize: 18,
            );
          }),
          Obx(() {
            if (!logic.isRecording.value) return const SizedBox();
            return Container(
              width: 5.w,
              height: 5.w,
              decoration: CommonDecoration.getShapeDecoration(
                bg: const Color(0XFFFF4B4B),
                radius: 5.w,
                borderColor: Colors.white,
                borderWidth: 1.w,
              ),
            );
          }).paddingLeft(5.w),
        ],
      ),
    );
  }

  Widget _buildCount() {
    return Container(
      width: 42,
      height: 42,
      decoration: CommonDecoration.getShapeDecoration(
        bg: '#626161'.toColor,
        borderWidth: 1,
        borderColor: Colors.white,
        radius: 12,
      ),
      alignment: Alignment.center,
      child: Obx(() {
        return TextWidget.oneLine(
          text: logic.count.value.toString(),
          color: Colors.white,
          fontSize: 18,
        );
      }),
    );
  }

  Widget _buildContent() {
    if (logic.cameraController == null || logic.cameraController?.value.isInitialized == false) {
      return Container();
    }
    return GestureDetector(
      onTap: logic.incrementCounter,
      child: CameraPreview(logic.cameraController!),
    );
  }

  Widget _buildBottom() {
    return Container(
      padding: EdgeInsets.symmetric(vertical: 14.w).safeBottom(),
      alignment: Alignment.center,
      child: ButtonWidget.icon(
        Assets.svgRecordVideoPause.toSvg(width: 60.w),
        onTap: logic.onSave,
      ),
    );
  }
}
